﻿using System;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using EmptyWallGallery.Core;
using EmptyWallGallery.Core.BLL;
using EmptyWallGallery.Core.Utilities;
using Microsoft.VisualBasic;

namespace EmptyWallGallery.FrontEnd.inside.Editor.Gallery
{
    public partial class Photo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            BindData();
        }

        public void BindData()
        {
            lstPhoto.DataSource = Images.Instance.GetAll(0, true);
            lstPhoto.DataBind();
        }

        protected string WriteImage(string fileName, DateTime createDate, string uploadBy)
        {
            var strImageDefault = fileName == ""
                                      ? "/images/no-image.jpg"
                                      : "/Uploads/" + uploadBy + "/" + createDate.Year + "/" +
                                        Common.Instance.Right("0" + createDate.Month, 2) + "/" +
                                        Common.Instance.Right("0" + createDate.Day, 2) + "/";

            return "<a href=\"" + strImageDefault + "700x0__" + fileName + "\" rel=\"TopProduct\"><img src=\"" +
                   strImageDefault + "135x0__" + fileName + "\" border=\"0\" width=\"130\" /></a>";
        }

        protected string WriteCreateDate(string toString)
        {
            return Strings.Format(DateTime.Parse(toString).AddHours(Configs.TimeLine),
                                  "dd-MM-yyyy");
        }

        protected void LstPhotoPreRender(object sender, EventArgs e)
        {

        }

        protected void DtPgPhotoPreRender(object sender, EventArgs e)
        {
            foreach (Control control in dtPgPhoto.Controls)
            {
                foreach (Control c in control.Controls)
                {
                    var currentLink = c as HyperLink;
                    if (currentLink == null) continue;

                    //temp = currentLink.Text;
                    //currentLink.Text = "<span>" + temp + "</span>";
                    var match = Regex.Match(currentLink.NavigateUrl, "Page=(?<Page123>.*?).*");

                    if (!match.Success) continue;

                    if (Request.QueryString["Page"] == null)
                    {
                        currentLink.NavigateUrl = Request.RawUrl + "?" + match.Value;
                    }
                    else
                    {
                        currentLink.NavigateUrl =
                            Request.RawUrl.Replace("Page=" + Request.QueryString["Page"], "") + match.Value;
                    }
                }
            }
        }
    }
}